﻿using UnityEngine;

public class UIListCell
{
    private Rect mRect;

    private object mObjDatas;
    private GameObject mObjPrefab;
    private GameObject mObjInstance;
    private XObjectPool mPool;
    private int mType;
    private Vector2? mSize;
    private int mIndex;

    public void display(Rect maskRect, RectTransform parent)
    {
        if (mRect.Overlaps(maskRect))
        {
            if (mObjInstance == null)
            {
                mObjInstance = mPool.pop(mType, mObjPrefab);
                (mObjInstance.transform as RectTransform).SetParent(parent, false);
                (mObjInstance.transform as RectTransform).anchoredPosition = new Vector2(mRect.x, -mRect.y);

                if (!mObjInstance.activeSelf)
                    mObjInstance.SetActive(true);

                onDisplay(mObjInstance.GetComponent<UIContext>(), mObjDatas);
            }
        }
        else
        {
            if (mObjInstance != null)
            {
                if (mObjInstance.activeSelf)
                    mObjInstance.SetActive(false);
                mPool.push(mType, mObjInstance);
                mObjInstance = null;

                onHide();
            }
        }
    }

    public void setPrefab(GameObject prefab)
    {
        mObjPrefab = prefab;
        mType = mObjPrefab.GetInstanceID();
    }

    public void setSize(Vector2? size = null)
    {
        mSize = size;
    }

    public void setDatas(object datas)
    {
        mObjDatas = datas;
    }

    public Vector2 getSize()
    {
        if (mSize != null && mSize.HasValue)
            return mSize.Value;
        return (mObjPrefab.transform as RectTransform).sizeDelta;
    }

    public void setRect(Vector2 point)
    {
        mRect = new Rect(point.x, point.y, getSize().x, getSize().y);
    }

    public virtual void clear()
    {
        if (mObjInstance != null)
        {
            if (mObjInstance.activeSelf)
                mObjInstance.SetActive(false);
            mPool.push(mType, mObjInstance);
            mObjInstance = null;
        }
        mObjPrefab = null;
        mSize = null;
    }

    public void setPool(XObjectPool pool)
    {
        mPool = pool;
    }

    public void setAnchor(Vector2 anchorMin, Vector2 anchorMax, Vector2 pivot)
    {
        var rt = mObjPrefab.transform as RectTransform;

        rt.anchorMin = anchorMin;
        rt.anchorMax = anchorMax;
        rt.pivot = pivot;
    }

    public Vector2 getPos()
    {
        return mRect.position;
    }

    public bool IsDisplaying
    {
        get
        {
            if (mObjInstance == null)
                return false;

            if (mObjInstance.activeSelf == false)
                return false;

            return true;
        }
    }

    public int getIndex()
    {
        return mIndex;
    }

    public void setIndex(int index)
    {
        mIndex = index;
    }

    protected virtual void onDisplay(UIContext context, object args)
    {
    }

    protected virtual void onHide()
    {
    }
}